//
// Created by 17775 on 2024/10/24.
//

#ifndef RAYCI_TIME_SERIES_VIEW_H
#define RAYCI_TIME_SERIES_VIEW_H

#include <QDockWidget>
#include "../qcustomplot.h"
#include "../RayCi/Single_CrossSection.h"
#include "../RayCi/Single_CrossSectionUI.h"
#include "../RayCi/LiveMode_Measurement.h"
#include "../RayCi/Single_PowerProfile.h"
#include "../RayCi/Single_PowerProfileUI.h"
#include "../LiveMode/livemode_powerprofile_options.h"
#include "../Single/Single_Histogram.h"
#include "../Single/Single_HistogramUI.h"
#include "../LiveMode/livemode_crosssection_options.h"

QT_BEGIN_NAMESPACE
namespace Ui { class Time_Series_View; }
QT_END_NAMESPACE

class Time_Series_View : public QDockWidget {
Q_OBJECT

signals:
    void setAutoContrast(bool checked);
    void closeTimeSeriesWindow();
    void reportTimeSeries();
    void showTimeSeriesOptions();

    void getCurrentY(double currentY);
    void updateEllipse(double x, double y, double radiusX, double radiusY, double angle);
    void TimeSeriesClicked();

//private slots:
//    void onMouseMove(const QMouseEvent *event) const;

private slots:
    void onMousePress(const QMouseEvent *event);
    void onMouseMove(const QMouseEvent *event);
    void onMouseRelease(const QMouseEvent *event);

public:
    explicit Time_Series_View(QWidget *parent = nullptr);
    ~Time_Series_View() override;
    void setWindowTitleName(const QString& name);

    static int nSizeX, nSizeY;
    void processCCDImageData(const QImage &image) const;
    void processFloatData(std::vector<float> &vecData);
    void setImageSize() const;
    void initEllipses(double dPositionX_px, double dPositionY_px, double dMajor_px, double dMinor_px) const;
    void setAuto(bool isAuto);

private:
    Ui::Time_Series_View *ui;
    QCPItemPixmap *imageItem = nullptr;
    QCustomPlot *_2DImagePlot = nullptr;
    QCPItemLine *centerHLine = nullptr;
    QCPItemLine *centerVLine = nullptr;
    QCPItemLine *xCrossSectionLine = nullptr;
    QCPItemText *xCrossSectionText = nullptr;
    QCPItemEllipse *ellipse = nullptr;
    QCPItemText *coordinateLabel = nullptr;
    QList<QCPItemEllipse *> ellipses;
    QMenu *m_contextMenu = nullptr;
    QCPItemLine *hArrow = nullptr;
    QCPItemLine *vArrow = nullptr;
    void set2DMapArea();
    void btn_AOI_Click();
    void btn_Adjust_Click();
    void btn_3DView_Click();
    void btn_CrossSection_Click() const;
    void btn_PowerProfile_Click() const;
    void btn_Histogram_Click();
    void btn_BeamResults_Click();
    void btn_Report_Click();
    std::vector<float> _vecData;
    float getDataAt(int x, int y) const;
    void closeEvent(QCloseEvent *event) override;

    LiveMode_Measurement *liveModeMeasurement = nullptr;
    Single_CrossSection *singleCrossSection = nullptr;
    Single_CrossSectionUI *singleCrossSectionUi = nullptr;
    Single_PowerProfile *singlePowerProfile = nullptr;
    Single_PowerProfileUI *singlePowerProfileUi = nullptr;
    Single_Histogram *singleHistogram = nullptr;
    Single_HistogramUI *singleHistogramUi = nullptr;
    LiveMode_PowerProfile_Options *singlePowerProfileOptions = nullptr;
    LiveMode_CrossSection_Options *singleCrossSectionOptions = nullptr;

    bool isDraggingXCrossSectionLine = false;
    bool isMoving = false;
    bool nearEllipseEdge = false;
    QPointF lastMousePos;
    double calculateDistanceToEllipseBoundary(const QPointF &mousePos) const;
};


#endif //RAYCI_TIME_SERIES_VIEW_H
